package liu.xml;

import java.util.List;

import liu.model.Mp3Info;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Mp3ListContentHandler extends DefaultHandler{
	private Mp3Info mp3info=null;
	private List<Mp3Info> infos=null;
	private String tagName=null;

	
	public List<Mp3Info> getInfos() {
		return infos;
	}

	public void setInfos(List<Mp3Info> infos) {
		this.infos = infos;
	}

	public Mp3ListContentHandler(List<Mp3Info> infos) {
		super();
		this.infos = infos;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String temp=new String(ch,start,length);
		if(tagName.equals("id")){
			mp3info.setId(temp);
		}
		else if(tagName.equals("mp3.name")){
			mp3info.setMp3Name(temp);
		}
		else if(tagName.equals("mp3.size")){
			mp3info.setMp3Size(temp);
		}
		else if(tagName.equals("lrc.name")){
			mp3info.setLrcName(temp);
		}
		else if(tagName.equals("lrc.size")){
			mp3info.setLrcSize(temp);
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(localName.equals("resource")){
			infos.add(mp3info);
		}
		tagName="";
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		tagName=localName;
		if(tagName.equals("resource")){
			mp3info=new Mp3Info();
		}	
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
	}
	
}
